package demon;
import java.util.Arrays;
public class MyArrayList implements IMyArrayList{
        public int useSize;
        public int[] arr;
        public MyArrayList(){
                this.arr=new int[10];
        }
        public boolean isFull(){
                if(arr.length==useSize){
                        return true;
                }
                return false;
        }
        private void Expanion(){
                this.arr= Arrays.copyOf(arr,arr.length*2);
        }
        private void checkPos(int pos)throws PosNotLegalException{
                if(pos<0||pos>useSize){
                        throw new PosNotLegalException();
                }
        }
        @Override
        public void add(int data) {
                if(isFull()){
                        Expanion();
                }
                this.arr[useSize]=data;
                useSize++;
        }
        @Override
        public void add(int pos, int data) {
                try{
                        checkPos(pos);
                }catch(PosNotLegalException e){
                        e.printStackTrace();
                }
                if(isFull()){
                        Expanion();
                }
                for (int i = this.useSize-1; i <pos ; i--) {
                        this.arr[i+1]=this.arr[i];
                }
                useSize++;

        }
        @Override
        public boolean contains(int toFind) {
                for (int i = 0; i < this.useSize; i++) {
                        if(toFind==arr[i]){
                                return true;
                        }
                }
                return false;
        }

        @Override
        public int indexOf(int toFind) {
                for (int i = 0; i < this.useSize; i++) {
                        if(toFind==this.arr[i]){
                                return i;
                        }
                }
                return -1;
        }

        @Override
        public int get(int pos) {
                try{
                        checkPos(pos);
                }catch (PosNotLegalException e){
                        e.printStackTrace();
                }
                return this.arr[pos];
        }

        @Override
        public void set(int pos, int value) {
                try {
                        checkPos(pos);
                }catch(PosNotLegalException e){
                        e.printStackTrace();
                }
                this.arr[pos]=value;
        }

        @Override
        public void remove(int toRemove) {
                int pos=indexOf(toRemove);
                if(pos==-1){
                        return;
                }
                for (int i = pos; i <useSize-1 ; i++) {
                        this.arr[i]=this.arr[i+1];
                }
                this.useSize--;
        }

        @Override
        public int size() {
                int count=0;
                for (int i = 0; i < this.useSize; i++) {
                        count++;
                }
                return count;
        }

        @Override
        public void clear() {
                this.useSize=0;
        }

        @Override
        public void display() {
                for (int i = 0; i < this.useSize; i++) {
                        System.out.print(arr[i]+" ");
                }
                System.out.println();
        }
}
